package com.shuhua.taskServer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuhua.common.result.Result;
import com.shuhua.pojo.entity.Craftsman;
import com.shuhua.pojo.entity.Team;
import com.shuhua.pojo.entity.TeamMember;
import com.shuhua.taskServer.mapper.TeamMapper;
import com.shuhua.taskServer.mapper.TeamMemberMapper;
import com.shuhua.taskServer.service.CraftsmanService;
import com.shuhua.taskServer.service.TeamMemberService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 班组成员服务实现类
 */
@Service
@RequiredArgsConstructor
public class TeamMemberServiceImpl extends ServiceImpl<TeamMemberMapper, TeamMember> implements TeamMemberService {
    
    private final TeamMapper teamMapper;
    private final CraftsmanService craftsmanService;
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<TeamMember> addTeamMember(Long teamId, Long craftsmanId) {
        // 验证班组是否存在
        Team team = teamMapper.selectById(teamId);
        if (team == null) {
            return Result.error("班组不存在");
        }
        
        // 验证工匠是否存在
        Craftsman craftsman = craftsmanService.getById(craftsmanId);
        if (craftsman == null) {
            return Result.error("工匠不存在");
        }
        
        // 检查是否已经是班组成员
        LambdaQueryWrapper<TeamMember> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TeamMember::getTeamId, teamId)
                .eq(TeamMember::getCraftsmanId, craftsmanId);
        long count = count(queryWrapper);
        if (count > 0) {
            return Result.error("该工匠已经是班组成员");
        }
        
        // 创建班组成员记录
        TeamMember teamMember = new TeamMember();
        teamMember.setTeamId(teamId);
        teamMember.setCraftsmanId(craftsmanId);
        teamMember.setJoinTime(new Date());
        save(teamMember);
        
        // 更新班组成员数量
        team.setMemberNum(team.getMemberNum() + 1);
        teamMapper.updateById(team);
        
        return Result.success("添加成员成功", teamMember);
    }
    
    @Override
    public Result<List<TeamMember>> getTeamMembers(Long teamId) {
        // 验证班组是否存在
        Team team = teamMapper.selectById(teamId);
        if (team == null) {
            return Result.error("班组不存在");
        }
        
        // 查询班组成员
        LambdaQueryWrapper<TeamMember> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TeamMember::getTeamId, teamId);
        List<TeamMember> members = list(queryWrapper);
        
        return Result.success(members);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> quitTeam(Long teamId, Long craftsmanId) {
        // 验证班组是否存在
        Team team = teamMapper.selectById(teamId);
        if (team == null) {
            return Result.error("班组不存在");
        }
        
        // 验证工匠是否存在
        Craftsman craftsman = craftsmanService.getById(craftsmanId);
        if (craftsman == null) {
            return Result.error("工匠不存在");
        }
        
        // 检查是否是班组成员
        LambdaQueryWrapper<TeamMember> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TeamMember::getTeamId, teamId)
                .eq(TeamMember::getCraftsmanId, craftsmanId);
        TeamMember teamMember = getOne(queryWrapper);
        
        if (teamMember == null) {
            return Result.error("该工匠不是班组成员");
        }
        
        // 检查是否是班组长
        if (team.getTeamLeaderId().equals(craftsmanId)) {
            return Result.error("班组长不能退出班组，请先转让班组长职位");
        }
        
        // 删除班组成员记录
        boolean removed = remove(queryWrapper);
        if (!removed) {
            return Result.error("退出班组失败");
        }
        
        // 更新班组成员数量
        team.setMemberNum(team.getMemberNum() - 1);
        teamMapper.updateById(team);
        
        return Result.success("退出班组成功", true);
    }
} 